package org.thirdeyetvg.ncanreader.activity.normal;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.thirdeyetvg.ncanreader.R;
import org.thirdeyetvg.ncanreader.helper.CustomEpubReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;

public class EpubNormalReaderActivity extends Activity {

	private static final String TAG = EpubNormalReaderActivity.class.getName();
	private CustomEpubReader reader;
	TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_text_normal_reader);
		Bundle extras = getIntent().getExtras();
		String fileName = extras.getString("fileName");
		Log.d(TAG, "fileName:" + fileName);
		tv = (TextView) findViewById(R.id.tv_text_reader);
		try {
			reader = new CustomEpubReader(fileName, tv);
			Log.d(TAG,"CustomEpubReader initialized");
			String content = reader.nextPage();
			tv.setText(content);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_text_normal_reader, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(TAG, "onKeyDown:keyCode:" + keyCode);
		String content;
		switch (keyCode) {

		case KeyEvent.KEYCODE_DPAD_DOWN:
			content = reader.nextPage();
			if (content != null) {
				tv.setText(content);
			}
			return true;

		case KeyEvent.KEYCODE_DPAD_UP:
			content = reader.previousPage();
			if (content != null) {
				tv.setText(content);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
